package com.xiaobai.zhuangxiuguanjia.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.xiaobai.zhuangxiuguanjia.R;
import com.xiaobai.zhuangxiuguanjia.Utils.MySingleton;
import com.xiaobai.zhuangxiuguanjia.model.Data;

import java.util.List;

/**
 * Created by gggcccwww on 2015/6/16.
 */
public class FindCompanyAdapter extends BaseAdapter{
    private Context context;
    private List<Data> datas;
    private RequestQueue requestQueue;
    private ImageLoader imageLoader;

    public FindCompanyAdapter(Context context, List<Data> datas) {
        this.context = context;
        this.datas = datas;
        MySingleton instance = MySingleton.getInstance(context);
        requestQueue=instance.getRequestQueue();
        imageLoader=instance.getImageLoader();
    }

    @Override
    public int getCount() {
        int ret=0;
        if (datas != null) {
            ret=datas.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret=null;
        if (datas != null) {
            ret=datas.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret=null;
        if(convertView!=null){
            ret=convertView;
        }else{
            ret= LayoutInflater.from(context).inflate(R.layout.findcompany_listview_item,null);
        }
        ViewHolder holder=(ViewHolder)ret.getTag();
        if(holder==null){
            holder=new ViewHolder();

            holder.cname= (TextView) ret.findViewById(R.id.company_name);
            holder.headerPhoto= (ImageView) ret.findViewById(R.id.company_logo);
            holder.mcase= (TextView) ret.findViewById(R.id.shejifangan);
            holder.koubei= (TextView) ret.findViewById(R.id.company_koubei);
            holder.viewnums= (TextView) ret.findViewById(R.id.company_zixunrenshu);
            holder.security= (ImageView) ret.findViewById(R.id.company_bao);
            holder.distance= (TextView) ret.findViewById(R.id.company_juli);

            ret.setTag(holder);
        }

        Data data = datas.get(position);

        holder.cname.setText(data.getCname());
        holder.mcase.setText("设计方案 : "+data.getmCase());
        holder.koubei.setText("口碑值 : "+data.getKoubei());
        holder.viewnums.setText("咨询人数 : "+data.getViewnums());
        if(data.getSecurity()==1){
            holder.security.setImageResource(R.mipmap.ico_content_orange);
        }
        holder.distance.setText(data.getDistance());
        String photoUrl = data.getHeadPhoto();
        imageLoader.get(photoUrl,ImageLoader.getImageListener(holder.headerPhoto,R.mipmap.company_default_logo,R.mipmap.company_default_logo));
        holder.distance.setText(data.getDistance());
        return ret;
    }

    private static class ViewHolder{
        public ImageView headerPhoto;
        public TextView cname;
        public TextView mcase;
        public TextView koubei;
        public TextView viewnums;
        public ImageView security;
        public TextView distance;
    }
}
